/**
 * 游戏的主程序入口
 * @author zhouzhanglin
 */
SmartyDeer = function()
{
	Sprite.call( this ) ;
	
	this._gameWorld ;
	this._startView ;
	this._passView ;
	this._blankLayer  ;
	
	//添加到舞台的事件侦听
	this.addEventListener(Event.ADDED_TO_STAGE , this.addedHandler);
};
FLASH.extend( SmartyDeer , Sprite);
//SmartyDeer.prototype = new Sprite(); //继承自Sprite
//SmartyDeer.prototype.constructor = SmartyDeer ;

SmartyDeer.prototype.addedHandler = function(e)
{
	var target = e.getTarget();
	target.removeEventListener(Event.ADDED_TO_STAGE , target.addedHandler);
	target.loadRes();
	
	//对全局事件进行处理
	target.globalEventHandler = function(e)
	{
		switch(e.getType() )
		{
			case GlobalEvent.NEXT_LEVEL:
			case GlobalEvent.GAME_START:
				GameSetting.currentLevel = new Level1();
				target.blankTween();
				//延迟
				setTimeout( function()
				{
					if(target._startView!=null ){
						target.removeChild(target._startView,true);
						target._startView = null ;
					}else if(target._passView!=null){
						target.removeChild(target._passView,true);
						target._passView = null ;
					}
					target._gameWorld=  new GameWorld();
					target.addChildAt( target._gameWorld,0 );
				},1000);
				break ;
			case GlobalEvent.GAME_OVER:
				target.blankTween();
				//延迟
				setTimeout( function()
				{
					target.removeChild(target._gameWorld,true);
					target._gameWorld = null ;
					target._startView = new StartView();
					target.addChildAt( target._startView,0 );
				},1000);
				break ;
			case GlobalEvent.GAME_PASS:
				target.blankTween();
				//延迟
				setTimeout( function()
				{
					target.removeChild(target._gameWorld,true);
					target._gameWorld = null ;
					target._passView = new PassView();
					target.addChildAt( target._passView,0 );
				},1000);
				break ;
		}
	};
	//侦听全局事件
	GlobalDispatcher.getInstance().addEventListener(GlobalEvent.GAME_START , target.globalEventHandler );
	GlobalDispatcher.getInstance().addEventListener(GlobalEvent.GAME_OVER , target.globalEventHandler );
	GlobalDispatcher.getInstance().addEventListener(GlobalEvent.NEXT_LEVEL , target.globalEventHandler );
	GlobalDispatcher.getInstance().addEventListener(GlobalEvent.GAME_PASS , target.globalEventHandler );
};

/**
 * 开始加载资源
 */
SmartyDeer.prototype.loadRes = function()
{
	var txt = new TextField();
	txt.align = TextFieldAlign.LEFT ;
	txt.font="bold 25px Arial";
	txt.color = "#ffffff"; //白色
	txt.text="SimpleText Test";
	txt.setX( this.getStage().getStageWidth()/2-40);
	txt.setY( this.getStage().getStageHeight()/2-10);
	this.addChild(txt);
	//===============
	//===============
	var _self = this ;
	function proHandler(e){
		txt.text = "loading  "+ ((e.getLoaded() / e.getTotal()*100) >>0)+"%" ;
	};
	function completeHandler(e){
		ResManager.getInstance().removeEventListener( ProgressEvent.PROGRESS , proHandler );
		ResManager.getInstance().removeEventListener( Event.COMPLETE , completeHandler );
		_self.removeAllChildren(true);
		_self.init();
		_self = null ;
	}
	ResManager.getInstance().addEventListener( ProgressEvent.PROGRESS , proHandler );
	ResManager.getInstance().addEventListener( Event.COMPLETE , completeHandler );
	ResManager.getInstance().startLoad();
};

SmartyDeer.prototype.init = function()
{
	//添加开始界面
	this._startView = new StartView();
	this.addChild(this._startView);
};

SmartyDeer.prototype.blankTween = function()
{
	if(!this._blankLayer){
		this._blankLayer = new BlankLayer();
		this.addChild(this._blankLayer);
	}
	this._blankLayer.visible=true ;
	this._blankLayer.setAlpha(0) ;
	//添加缓动
	var _self = this ;
	JSTweener.addTween(_self._blankLayer, {
	      time: 1,
	      transition: 'linear',
	      _alpha:1,
	      onComplete:  tweenCom 
	  });
	
	function tweenCom()
	{
		if(_self._blankLayer.getAlpha()==1){
			JSTweener.addTween(_self._blankLayer, {
			      time: 0.6 ,
			      transition: 'linear',
			      _alpha:0,
			      onComplete:  tweenCom 
			  });
		}else{
			_self._blankLayer.visible=false ;
		}
	}
};

SmartyDeer.prototype.dispose = function()
{
	Sprite.prototype.dispose.call(this);
	GlobalDispatcher.getInstance().removeEventListener(GlobalEvent.GAME_START , this.globalEventHandler );
	GlobalDispatcher.getInstance().removeEventListener(GlobalEvent.GAME_OVER , this.globalEventHandler );
	GlobalDispatcher.getInstance().removeEventListener(GlobalEvent.NEXT_LEVEL , this.globalEventHandler );
	GlobalDispatcher.getInstance().removeEventListener(GlobalEvent.GAME_PASS , this.globalEventHandler );
	this._gameWorld  = null ;
	this._startView = null ;
	this._blankLayer = null ;
	this._passView = null ;
};